package cac.fenjian.resource.mspartshelvesbasic.rest;

import cac.fenjian.resource.MsPartShelves.dto.MsPartShelvesBasicDTO;
import cac.fenjian.resource.mspartshelvesbasic.service.MsPartShelvesBasicService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

@RestController
@RequestMapping("/msPartShelves/")
public class MsPartShelvesBasicRest {

    @Autowired
    private MsPartShelvesBasicService msPartShelvesBasicService;
    //大件货物查询
    @PostMapping("/searchByPage")
    public ResponseMsg<QueryRespBean<HashMap>> searchByPage(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg<>();
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        HashMap searchParams = JSON.parseObject(searchParamsParams, HashMap.class);

        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), HashMap.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            HashMap searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<HashMap>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<HashMap> result = msPartShelvesBasicService.searchMsPartShelvesBasicByPage(queryReqBean, ContextCommonHolder.getOrgId(),queryReqBean.getSortExp());
        List<HashMap> result1 = msPartShelvesBasicService.searchMsPartShelvesBasicByPage(searchParams, ContextCommonHolder.getOrgId(),queryReqBean.getSortExp());
        if(result1.size()==0){
            responseMsg.setRetCode("201").setMessage("未查询到数据").setResponseBody(result);
        }
        else responseMsg.setResponseBody(result);
        return  responseMsg;
    }

    //新增大件货物表信息
    @PostMapping("/save")
    public ResponseMsg<Map> save(@RequestBody HashMap<String,Object> map){
        ResponseMsg<Map> responseMsg =  new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        try {
            msPartShelvesBasicService.insertMsPartShelvesBasic(map);
            s++;
        }catch(Exception e)
        {
            e.printStackTrace();
            f++;
        }
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("新增失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("新增成功！");
        }
        return responseMsg;
    }
    //更新大件货物表数据
    @PostMapping("/update")
    public ResponseMsg<Map> updateMsPartShelvesBasic(@RequestBody HashMap<String,Object> map){
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        if(map.get("wmsLocatorId")!= null){
            try {
                msPartShelvesBasicService.updateMsPartBasic(map);
                s++;
            }catch(Exception e)
            {
                e.printStackTrace();
                f++;
            }
        }
        else f++;
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("修改失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("修改成功！");
        }
        return responseMsg;
    }

    //按照id删除大件货物表中信息
    @PostMapping("/delete")
    public ResponseMsg<Map> deleteById(@RequestBody HashMap<String,Object> map) {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        List<String> ids = (List<String>) map.get("wmsLocatorIds");
        if(ids.size()==0){
            return responseMsg.setRetCode("201").setMessage("请选择需要删除的数据");
        }
        for(int i = 0 ;i<ids.size();i++){
            String id = ids.get(i);
            if(id!=null){
                msPartShelvesBasicService.deleteMsPartShelvesBasic(id);
                s++;
            }
            else f++;
        }
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("删除失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("删除成功！");
        }
        if (s != 0 && f != 0) {
            responseMsg.setRetCode("202");
            responseMsg.setMessage("删除有失败项！");
            responseMsg.setResponseBody(reMsg);
        }
        return responseMsg;
    }
}
